package demo.socket.protocol;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSock {
	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(1234);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Socket clientSocket = null;
		try {
			clientSocket = serverSocket.accept();
		} catch (Exception e) {
			e.printStackTrace();
		}
		PrintWriter out = null;
		BufferedReader in = null;
		try {
			out = new PrintWriter(clientSocket.getOutputStream());
			in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String inputLine, outputLine;
		ProtocolSock protocolSock = new ProtocolSock();
		outputLine = protocolSock.processIn(null);
		out.print(outputLine);
		try {
			inputLine = in.readLine();
			while(inputLine != null){
				outputLine = protocolSock.processIn(inputLine);
				out.print(inputLine);
				if (outputLine.equals("Bye.")) {
					break;
				}
			}
			in.close();
			out.close();
			clientSocket.close();
			serverSocket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
